package com.supplychainapi.fastdfs;

import com.joindata.inf.common.util.log.Logger;
import com.supplychainapi.config.FastdfsConfig;
import com.supplychainapi.fastdfs.client.AbstractFastDfsClient;
import com.supplychainapi.fastdfs.client.TrackerServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangziqi on 2017/10/17.
 */
@Slf4j
@Configuration
@Component
public class BuildFastDfs {

    @Autowired
    private FastdfsConfig config;

    private static final Logger log = Logger.get();

    @Bean
    public FastDfsClient buildClient()
    {
        AbstractFastDfsClient.Builder builder = AbstractFastDfsClient.newBuilder();
        builder.connectTimeout(Long.parseLong(config.getConnectTimeout()));
        builder.readTimeout(Long.parseLong(config.getReadTimeout()));

        //HostPort[] hostPorts = NetworkUtil.parseHostPort(properties.getTracker_server());

        List<TrackerServer> serverList = new ArrayList<>();
        serverList.add(new TrackerServer(config.getTrackerServer(), Integer.parseInt(config.getTrackerPort())));
        /*for(HostPort hostPort: hostPorts)
        {
        }*/

        builder.trackers(serverList);

        log.info("FastDFS Tracker 地址: {}", config.getTrackerServer());

        return new FastDfsClient(builder);
    }
}
